/**
 * Basic renderer - given pos, indices, normal, tex idx, tex coord
 */
define(
["framework/graphic/WebGL", "framework/asset/AssetSyst"],
function(WebGL, AssetSyst) {
    return {
        getName: function() {
            return "SYSTEM - BASIC RENDER";
        },

        create: function(systemMgr, shaderUrl) {
            var _batches = [];
            var _CLASS = this;
            var _shader = AssetSyst.get(shaderUrl)

            var _self = {
                getClass: function() {
                    return _CLASS;
                },
                
                add: function(batch) {
                    _batches.push(batch);
                },

                rem: function(batch) {
                    _batches.splice(_batches.indexOf(batch),1);
                },

                init: function() {
                    //Does nothing
                },

                destroy: function() {
                    //Release shader
                    AssetSyst.let(_shader);

                    //TODO Destroy all batch
                },

                render: function(cam) {
                    //Skip if shader has not ready
                    if(!_shader.isLoaded())
                        return;

                    //Use program
                    WebGL.gl.useProgram(_shader.program);

                    //Set camera and lights and other system-wide settings
                    WebGL.gl.uniformMatrix4fv(_shader.getUniform("uVMat"), false, cam.getView());
                    WebGL.gl.uniformMatrix4fv(_shader.getUniform("uPMat"), false, cam.getPerspective());
                    WebGL.gl.uniform3fv(_shader.getUniform("uCamPos"), cam.pos);

                    WebGL.gl.uniform3fv(_shader.getUniform("uLDir"), [-1,-2,-3]);
                    WebGL.gl.uniform4fv(_shader.getUniform("uLAmbient"), [1,1,1,1]);
                    WebGL.gl.uniform4fv(_shader.getUniform("uLDiffuse"), [1,1,1,1]);
                    WebGL.gl.uniform4fv(_shader.getUniform("uLSpecular"), [1,1,1,1]);

                    //Render each batch
                    for(var i = 0; i < _batches.length; ++i)
                        _batches[i].render(_shader);
                }
            };

            //Register to system
            systemMgr.addSystem(_self);

            return _self;
        }
    };
});